package com.study.testShiro.service.impl;

import java.time.LocalDateTime;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import com.study.testShiro.dao.IUserDao;
import com.study.testShiro.entity.User;
import com.study.testShiro.service.IUserService;
import com.study.testShiro.utils.EncryptUtil;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired
	private IUserDao userDao;
	
	@Override
	public User findUserByName(String name) {
		User user = new User();
		user.setUserName(name);
		Example<User> example = Example.of(user);
		Optional<User> optional = userDao.findOne(example);
		return optional.orElse(null);
	}

	@Override
	public void addUser(String username, String password) {
		User user = new User();
		user.setUserName(username);
		user.setCreateTime(LocalDateTime.now());
		user.setSalt(EncryptUtil.RodomSalt());
		user.setName("wuyu");
		user.setState((byte)1);
		password = EncryptUtil.getPass(password, user.getSalt(), 2, "md5");
		System.out.println(password);
		user.setPassword(password);
		userDao.save(user);
	}

	
}
